﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace BingMaps.View
{
    using System.IO;
    using System.Windows.Media.Imaging;

    using Microsoft.Xna.Framework.Media;

    public partial class LocationPage : PhoneApplicationPage
    {
        public LocationPage()
        {
            InitializeComponent();
        }

        public static void SaveToMediaLibrary(
    FrameworkElement element,
    string title)
        {
            try
            {
                var bmp = new WriteableBitmap(element, null);

                var ms = new MemoryStream();
                bmp.SaveJpeg(
                    ms,
                    (int)element.ActualWidth,
                    (int)element.ActualHeight,
                    0,
                    100);
                ms.Seek(0, SeekOrigin.Begin);

                var lib = new MediaLibrary();
                var filePath = string.Format(title + ".jpg");
                lib.SavePicture(filePath, ms);

                MessageBox.Show(
                    "Saved in your media library!",
                    "Done",
                    MessageBoxButton.OK);
            }
            catch
            {
                MessageBox.Show(
                    "There was an error. Please disconnect your phone from the computer before saving.",
                    "Cannot save",
                    MessageBoxButton.OK);
            }
        }

        private void ScreenShot_Click(object sender, RoutedEventArgs e)
        {
            SaveToMediaLibrary(this.ByPoint, "Bing Location");
        }
    }
}